package cn.wsh.basic.controller;

/**
 * @ClassName : FastDfsController
 * Author : "吴----"
 * @date : 2022/2/11 20:17
 */
import cn.wsh.basic.util.AjaxResult;
import cn.wsh.basic.util.FastdfsUtil;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {
    //文件的上传接口
    @PostMapping
    public AjaxResult upload(@RequestPart(value = "file",required = true) MultipartFile file){
        String filename = file.getOriginalFilename();
        String extName = filename.substring(filename.lastIndexOf(".")+1 );
        try {
            String path = FastdfsUtil.upload(file.getBytes(), extName);
            System.out.println(path);
            return AjaxResult.me().setResultObj(path);
        } catch (IOException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙，稍后重试！！！");
        }
    }

    //文件的删除接口
    @DeleteMapping
    public AjaxResult delete(@RequestParam(value = "path",required = true) String path){

        try {
            String pathTmp = path.substring(1); // group1/M00/02/6E/rBE3kWIGIA6AE1A6AAFeHybleuQ447.jpg
            String groupName =  pathTmp.substring(0, pathTmp.indexOf("/")); //group1
            String remotePath = pathTmp.substring(pathTmp.indexOf("/")+1);// M00/02/6E/rBE3kWIGIA6AE1A6AAFeHybleuQ447.jpg
            System.out.println(groupName);
            System.out.println(remotePath);
            FastdfsUtil.delete(groupName,remotePath);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("删除失败！"+e.getMessage());
        }
    }
}
